﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using OpenQuarters.WebQuarters.Utils;
using System.IO;

namespace OpenQuarters.WebQuarters.Core
{
    public class CSS
    {
        public static CMSPageVersion PageContext
        {
            get { return ContextItem.GetItem("CSS.PageContext", () => CMSPage.Current); }
            set { ContextItem.SetItem("CSS.PageContext", value); }
        }

        /// <summary>
        /// Page stylesheets, indexed by Page Version ID
        /// </summary>
        public static LimitedDictionary<long, ResourceDescriptorCollection> PageStyleSheets
        {
            get
            {
                return SessionItem.GetItem(
                    "__CMS_" + CSSType.PageStyleSheet.ToString() + "s",
                    () => new LimitedDictionary<long, ResourceDescriptorCollection>(20)
                );
            }
        }

        /// <summary>
        /// Admin stylesheets, indexed by Page Version ID
        /// </summary>
        public static LimitedDictionary<long, ResourceDescriptorCollection> AdminStyleSheets
        {
            get
            {
                return SessionItem.GetItem(
                    "__CMS_" + CSSType.AdminStyleSheet.ToString() + "s",
                    () => new LimitedDictionary<long, ResourceDescriptorCollection>(20)
                );
            }
        }

        public static void RegisterStyleSheet(string fileName, string assembly, string filePath, CSSType cssType)
        {
            RegisterStyleSheet(new ResourceDescriptor(fileName, assembly, filePath, null), cssType);
        }

        public static void RegisterStyleSheet(ResourceDescriptor resource, CSSType cssType)
        {
            LimitedDictionary<long, ResourceDescriptorCollection> styles = null;

            switch (cssType)
            {
                case CSSType.AdminStyleSheet:
                    styles = AdminStyleSheets;
                    break;
                case CSSType.PageStyleSheet:
                    styles = PageStyleSheets;
                    break;
            }

            styles[PageContext.PageVersionId].Add(resource);
        }

        public static void ClearStyleSheets()
        {
            PageStyleSheets[PageContext.PageVersionId].Clear();
            AdminStyleSheets[PageContext.PageVersionId].Clear();
        }

        public static string ConcatScripts(ResourceDescriptorCollection styles)
        {
            StringBuilder sb = new StringBuilder();
            StreamReader reader;
            if (styles != null)
            {
                foreach (var style in styles)
                {
                    try
                    {
                        sb.AppendLine("");
                        sb.AppendLine("//Start of stylesheet: " + style.Key);
                        reader = new StreamReader(Resource.GetEmbeddedFile(style.Value.Assembly, style.Value.FilePath));
                        sb.Append(reader.ReadToEnd());

                    }
                    catch (FileNotFoundException)
                    {
                        sb.AppendLine("//File not found: " + style.Value.Assembly + ", " + style.Value.FilePath);
                    }
                    catch (IOException)
                    {
                        sb.AppendLine("//File not found: " + style.Value.Assembly + ", " + style.Value.FilePath);
                    }
                    finally
                    {
                        sb.AppendLine("//End of script: " + style.Key);
                        sb.AppendLine("");
                    }
                }
            }

            return sb.ToString();
        }
    }

    public enum CSSType
    {
        PageStyleSheet,
        AdminStyleSheet
    }
}